JavaServer Pages

JavaServer Pages (JSP) is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 as Sun's answer to ASP and PHP, JSP was designed to address the perception that the Java programming environment didn't provide developers with enough support for the Web.

To deploy and run, a compatible web server with servlet container is required. The Java Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystems and the JCP (Java Community Process) must both be supported by the container.

Contents

Overview

Architecturally, JSP may be viewed as a high-level abstraction of Java servlets. JSPs are loaded in the server and are operated from a structured special installed Java server packet called a Java EE Web Application, often packaged as a file archive.

JSP allows Java code and certain pre-defined actions to be interleaved with static web markup content, with the resulting page being compiled and executed on the server to deliver an HTML or XML document. The compiled pages and any dependent Java libraries use Java bytecode rather than a native software format, and must therefore be executed within a Java virtual machine (JVM) that integrates with the host operating system to provide an abstract platform-neutral environment.

JSP syntax is a fluid mix of two basic content forms: scriptlet elements and markup. Markup is typically standard HTML or XML, while scriptlet elements are delimited blocks of Java code which may be intermixed with the markup. When the page is requested the Java code is executed and its output is added, in situ, with the surrounding markup to create the final page. JSPs must be compiled to Java bytecode classes before they can be executed, but such compilation is needed only when a change to the source JSP file has occurred.

Java code is not required to be complete (self contained) within its scriptlet element block, but can straddle markup content providing the page as a whole is syntactically correct (for example, any Java if/for/while blocks opened in one scriptlet element must be correctly closed in a later element for the page to successfully compile). This system of split inline coding sections is called step over scripting because it can wrap around the static markup by stepping over it. Markup which falls inside a split block of code is subject to that code, so markup inside an if block will only appear in the output when the if condition evaluates to true; likewise markup inside a loop construct may appear multiple times in the output depending upon how many times the loop body runs.

The JSP syntax adds additional XML-like tags, called JSP actions, to invoke built-in functionality. Additionally, the technology allows for the creation of JSP tag libraries that act as extensions to the standard HTML or XML tags. JVM operated tag libraries provide a platform independent way of extending the capabilities of a web server. Note that not all commercial Java servers are Java EE specification compliant.

Starting with version 1.2 of the JSP specification, JavaServer Pages have been developed under the Java Community Process. JSR 53 defines both the JSP 1.2 and Servlet 2.3 specifications and JSR 152 defines the JSP 2.0 specification. As of May 2006 the JSP 2.1 specification has been released under JSR 245 as part of Java EE 5. As of Dec 10, 2009 the JSP 2.2 specification has been released as a maintenance release of JSR 245.

Example

JSPs are compiled into servlets by a JSP compiler. The compiler either generates a servlet in Java code that is then compiled by the Java compiler, or it may compile the servlet to byte code which is directly executable. JSPs can also be interpreted on-the-fly, reducing the time taken to reload changes.

Regardless of whether the JSP compiler generates Java source code for a servlet or emits the byte code directly, it is helpful to understand how the JSP compiler transforms the page into a Java servlet. For example, consider the following input JSP and its resulting generated Java Servlet.

Input JSP

 <%@ page errorPage="myerror.jsp" %>
 <%@ page import="com.foo.bar" %>
 
 <html>
 <head>
 <%! int serverInstanceVariable = 1;%>
 
 <% int localStackBasedVariable = 1; %>
 <table>
 <tr><td><%= toStringOrBlank( "expanded inline data " + 1 ) %></td></tr>

Resulting servlet

 package jsp_servlet;
 import java.util.*;
 import java.io.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 import javax.servlet.jsp.*;
 import javax.servlet.jsp.tagext.*;
 
 import com.foo.bar; // Imported as a result of <%@ page import="com.foo.bar" %>
 importclass _myservlet implements javax.servlet.Servlet, javax.servlet.jsp.HttpJspPage {
    // Inserted as a
    // result of <%! int serverInstanceVariable = 1;%>
    int serverInstanceVariable = 1;public void _jspService( javax.servlet.http.HttpServletRequest request,
    javax.servlet.http.HttpServletResponse response )
    throws javax.servlet.ServletException,
    java.io.IOException
    {
        javax.servlet.ServletConfig config =; // Get the servlet config
        Object page = this;
        PageContext pageContext =;            // Get the page context for this request
        javax.servlet.jsp.JspWriter out = pageContext.getOut();
        HttpSession session = request.getSession( true );
        try {
            out.print( "<html>\r\n" );
            out.print( "<head>\r\n" );// From <% int localStackBasedVariable = 1; %>
            int localStackBasedVariable = 1;
            …
            out.print( "<table>\r\n" );
            out.print( " <tr><td>" );
            // From <%= toStringOrBlank( "expanded inline data " + 1 ) %>
            out.print( toStringOrBlank( "expanded inline data " + 1 ) );
            out.print( " </td></tr>\r\n" );} catch ( Exception _exception ) {
            // Clean up and redirect to error page in <%@ page errorPage="myerror.jsp" %>
        }
    }
 }

JSP 2.0

The new version of the JSP specification includes new features meant to improve programmer productivity. Namely:

The Java EE 5 Platform has focused on easing development by making use of Java language annotations that were introduced by J2SE 5.0. JSP 2.1 supports this goal by defining annotations for dependency injection on JSP tag handlers and context listeners.

Another key concern of the Java EE 5 specification has been the alignment of its web tier technologies, namely JavaServer Pages (JSP), JavaServer Faces (JSF), and the JavaServer Pages Standard Tag Library (JSTL).

The outcome of this effort has been the Unified Expression Language (EL), which integrates the expression languages defined by JSP 2.0 and JSF 1.1.

The main key additions to the Unified EL that came out of the alignment work have been: A pluggable API for resolving variable references into Java objects and for resolving the properties applied to these Java objects, support for deferred expressions, which may be evaluated by a tag handler when needed, unlike their regular expression counterparts, which get evaluated immediately when a page is executed and rendered, and support for l-value expression, which appear on the left hand side of an assignment operation. When used as an l-value, an EL expression represents a reference to a data structure, for example: a JavaBeans property, that is assigned some user input. The new Unified EL is defined in its own specification document, which is delivered along with the JSP 2.1 specification.

Thanks to the Unified EL, JSTL tags, such as the JSTL iteration tags, can now be used with JSF components in an intuitive way.

JSP 2.0 introduced a problem in the tag library section on how the JSP version information was represented. The specification itself is inconsistent, sometimes referring to a jsp-version element, and at other times a version attribute on the root element. JSF specifications have gone with the later interpretation; however some JSP implementations still expect the jsp-version element.

JSP 2.1 leverages the Servlet 2.5 specification for its web semantics.

See also

References

Further reading

External links